<?php

namespace App\Admin\Controllers;

use App\Admin\Actions\Grid\GenerateKeys;
use App\Admin\Repositories\CardNum;
use App\Models\CardType;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class CardNumController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new CardNum(['card']), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('card.name','卡名称');
            $grid->column('card.images','卡封面')->image('', 50, 50);
            $grid->column('key','密钥');
            $grid->column('status')->using([1 => '已使用', 0 => '未使用'])->label([
                1=>'success',
                0=>'danger'
            ]);
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();
            $grid->selector(function (Grid\Tools\Selector $selector) {
                $selector->select('status', '状态', [
                    1 => '已使用',
                    0 => '未使用',
                ]);
                $selector->select('card_id', '卡名称', CardType::pluck('name', 'id'));
            });
            $grid->quickSearch(['key','card.name']);
            $grid->tools(function (Grid\Tools $tools){
                $tools->append(new GenerateKeys());
            });
            $titles = ['id' => 'ID', 'card.name' => '名称', 'key' => '密钥'];

            $grid->export($titles)->filename('卡密钥列表');
            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');

            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new CardNum(), function (Show $show) {
            $show->field('id');
            $show->field('card_id');
            $show->field('dense');
            $show->field('status');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new CardNum(), function (Form $form) {
            $form->display('id');
            $form->select('card_id')->options(CardType::pluck('name', 'id'));
            $form->text('key','密钥')->maxLength(32)->help('请输入32位随机字符串');
            $form->hidden('status')->value(0);

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
